<?php
namespace model;

class cat{
    
    use \dk\traits\Singleton;

    protected $table;

    public function __construct(){
        $this->table = $this->getCatTable();
    }
    
    protected static $tableInstance;
    /**
    * put your comment there...
    * 
    * @return \sp\db\Table
    */
    public function getCatTable(){
        if(isset(self::$tableInstance)){
            return self::$tableInstance;
        }
        $table = $this->getTable();
        if($table){
            self::$tableInstance = $table;
        }
        return $table;
    }
    public function getChild($id){
        $where = ['pid' => $id];
        return $this->find($where);
    }
    
    public function hasChild($id){
        $where = ['pid' => $id];
        return $this->count($where);
    }

    /**
    * put your comment there...
    * 
    * @return \sp\db\Table
    */
    public function getTable(){}
    
    public function getInfo($where){
        if(is_numeric($where)){
            $where = array('id' => $where);
        }
        $where['_limit'] = 1;
        $where = $this->formatWhere($where);
        return $this->find($where)->current();
    }
    /**
     * @param array $where
     * @return \sp\db\mysql\ResultSet
     */
    public function find(array $where = array()){
        $where = $this->formatWhere($where);
        return $this->table->select($where);
    }
    
    public function count(array $where = array()){
        $where = $this->formatWhere($where);
        return $this->table->count($where);
    }
    
    public function update($post, $where, $mask = 1){
        $post = $this->filter($post, false);
        $where = $this->formatWhere($where);
        return $this->table->update($post, $where, $mask);
    }
    /**
     * @param array $where
     */
    public function delete(array $where){
        $where = $this->formatWhere($where);
        return $this->table->delete($where);
    }
    /**
     * @param array $where
     */
    public function insert(array $post){
        $post = $this->filter($post, true);
        return $this->table->insert($post);
    }
    
    protected function filter($post, $isAdd= false){
        return $post;
    }

    protected function formatWhere($where){
        return $where;
    }
}


